package com.sicnustray.controller;


import com.sicnustray.common.Constants;
import com.sicnustray.common.Result;
import com.sicnustray.entity.Dict;
import com.sicnustray.mapper.DictMapper;
import com.sicnustray.utils.annotation.MyLog;
import com.sicnustray.entity.Menu;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import java.util.List;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;

import com.sicnustray.service.IMenuService;

import org.springframework.web.bind.annotation.RestController;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author yl
 * @since 2022-11-09
 */
@RequiresPermissions("菜单管理")
@RestController
@RequestMapping("/menu")
public class MenuController {
    
        @Resource
        private IMenuService menuService;

        @Resource
        private DictMapper dictMapper;

        // 新增或者更新
        @MyLog(value = "新增或更新菜单")
        @PostMapping
        public Result save(@RequestBody Menu menu) {
                return Result.success(menuService.saveOrUpdate(menu));
                }

        @MyLog(value = "单个删除菜单")
        @DeleteMapping("/{id}")
        public Result delete(@PathVariable Integer id) {
                return Result.success(menuService.removeById(id));
                }

        @MyLog(value = "批量删除菜单")
        @PostMapping("/del/batch")
        public Result deleteBatch(@RequestBody List<Integer> ids) {
                return Result.success(menuService.removeByIds(ids));
                }

        @MyLog(value = "获取所有菜单ID")
        @GetMapping("/ids")
        public Result findAllIds(){
                return Result.success(menuService.list().stream().map(Menu::getId));
        }

        @MyLog(value = "查找二级菜单")
        @GetMapping
        public Result findAll(@RequestParam String name  ) {
                return Result.success(menuService.findMenus(name));
                }

        @MyLog(value = "查找单个菜单")
        @GetMapping("/{id}")
        public Result findOne(@PathVariable Integer id) {
                return Result.success(menuService.getById(id));
                }

//        @GetMapping("/page")
//        public Result findPage(@RequestParam Integer pageNum,
//                                 @RequestParam Integer pageSize,
//                               @RequestParam String name       ) {
//                QueryWrapper<Menu> queryWrapper = new QueryWrapper<>();
//                queryWrapper.like("name",name);
//               // queryWrapper.orderByDesc("id");
//                return Result.success(menuService.page(new Page<>(pageNum, pageSize), queryWrapper));
//                }
        @MyLog(value = "获取图标")
        @GetMapping("icons")
        public Result getIcons(){
                QueryWrapper<Dict> queryWrapper=new QueryWrapper<>();
                queryWrapper.eq("type", Constants.DICT_TYPE_ICON);
               return Result.success(dictMapper.selectList(queryWrapper));
        }
}

